package com.yang.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.annotation.OptLogger;
import com.yang.domain.dto.Result;
import com.yang.domain.entity.Shop;
import com.yang.service.IShopService;
import com.yang.constants.SystemConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.yang.constants.OptTypeConstants.ADD;
import static com.yang.constants.OptTypeConstants.UPDATE;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yang
 * @since 2022-10-22
 */
@Api("商品模块")
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Resource
    public IShopService shopService;

    /**
     * 根据id查询商铺信息
     * @param id 商铺id
     * @return 商铺详情数据
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询商铺信息")
    public Result queryShopById(@PathVariable("id") Long id) {
        Shop shop = shopService.queryShopById(id);
        return Result.ok(shop);

    }

    /**
     * todo 周宇辉 2
     * 新增商铺信息
     * @param shop 商铺数据
     * @return 商铺id
     */
    @PostMapping
    @OptLogger(value = ADD)
    @ApiOperation(value = "新增店铺信息")
    public Result saveShop(@RequestBody Shop shop) {
        // 写入数据库
        shopService.save(shop);
        // 返回店铺id
        return Result.ok(shop.getId());
    }

    /**
     * 更新商铺信息
     * @param shop 商铺数据
     * @return 无
     */
    @PutMapping
    @ApiOperation(value = "修改店铺信息")
    @OptLogger(value = UPDATE)
    public Result updateShop(@RequestBody Shop shop) {
        shopService.updateShop(shop);
        return Result.ok();
    }


    /**
     * 根据商铺名称关键字分页查询商铺信息
     * @param name 商铺名称关键字
     * @param current 页码
     * @return 商铺列表
     */
    @GetMapping("/of/name")
    @ApiOperation(value = "根据商铺名称关键字分页查询商铺信息")
    public Result queryShopByName(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "current", defaultValue = "1") Integer current
    ) {
        // 根据类型分页查询
        Page<Shop> page = shopService.query()
                .like(StrUtil.isNotBlank(name), "name", name)
                .page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 返回数据
        return Result.ok(page.getRecords());
    }

    /**
     * 基于Redis的GEO实现的附近店铺功能
     * @param typeId 商品类型
     * @param current 页码号
     * @param x 经度
     * @param y 维度
     * @return 符合要求的商户信息
     */
    @GetMapping("/of/type")
    @ApiOperation(value = "基于Redis的GEO实现的附近店铺功能")
    public Result queryShopByType(
            @RequestParam("typeId") Integer typeId,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "x", required = false) Double x,
            @RequestParam(value = "y", required = false) Double y
    ) {
        List<Shop> shops = shopService.queryShopByType(typeId, current, x, y);

        return Result.ok(shops);
    }
}
